/**
 * This class manages the initial creation of the database and its tables
 * The onCreate method is only called if the database does not already exist
 * In the onCreate method, the Location table is created.
 */

package com.firstresponder.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class FirstResponderDBHelper extends SQLiteOpenHelper {

	public FirstResponderDBHelper(Context context, String name, CursorFactory cursorFactory, int version)
	{
		super(context, name, cursorFactory, version);
		Log.d("FirstResponderDBHelper", "in constructor");
		
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.i("FirstResponderDBHelper", "Creating tables");
		try {
			createLocationTable(db);
	//		createApparatusTable(db);
		}
		catch (SQLiteException ex)
		{
			Log.e("FirstResponderDBHelper", Log.getStackTraceString(ex));
		}

	}

	private void createLocationTable(SQLiteDatabase db) {
		Log.d("FirstResponderDBHelper", "creating location table");
		String queryString = "create table " + 
		                     DBConstants.LocationConstants.LOCATION_TABLE +
		                     " (" + DBConstants.LocationConstants.LOCATION_KEY + 
		                     " integer primary key, " + 
		                     DBConstants.LocationConstants.DEPT_ID +
		                     " text not null, " + 
		                     DBConstants.LocationConstants.SHORT_NAME +
		                     " text not null, " +
		                     DBConstants.LocationConstants.MAC_ADDR +
		                     " text, " + 
		                     DBConstants.LocationConstants.CURR_STATUS +
		                     " text, " + 
		                     DBConstants.LocationConstants.TYPE +
		                     " text,  " +
		                     DBConstants.LocationConstants.PRIORITY +
		                     " integer, " +
		                     DBConstants.LocationConstants.CURR_LAT +
		                     " real, " + 
		                     DBConstants.LocationConstants.CURR_LONG +
		                     " real);  ";
		  Log.d("FirstResponderDBHelper", "will execute this query: "+ queryString);             
		  db.execSQL(queryString);      
		                     
		
	}
	
	/*private void createApparatusTable(SQLiteDatabase db) {
		String queryString = "create table " + 
		                     DBConstants.ApparatusConstants.APPARATUS_TABLE +
		                     " (" + DBConstants.ApparatusConstants.APPARATUS_KEY + 
		                     " integer primary key autoincrement, " + 
		                     DBConstants.ApparatusConstants.DEPT_ID +
		                     " text not null, " + 
		                     DBConstants.ApparatusConstants.LONG_NAME +
		                     " text, " + 
		                     DBConstants.ApparatusConstants.SHORT_NAME +
		                     " text not null, " + 
		                     DBConstants.ApparatusConstants.MAC_ADDR +
		                     " text, " + 
		                     DBConstants.ApparatusConstants.STATUS +
		                     " text, " + 
		                     DBConstants.ApparatusConstants.TYPE +
		                     " text ); ";
		                     
		  db.execSQL(queryString);      
		                     
		
	}*/

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w("FirstResponderDB","Upgrading from version " + oldVersion + " to version " + 
				          newVersion + "will destroy all data");
		db.execSQL("drop table if exists " + DBConstants.LocationConstants.LOCATION_TABLE);
	//	db.execSQL("drop table if exists " + DBConstants.ApparatusConstants.APPARATUS_TABLE);
		onCreate(db);

	}

}
